﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Profile_Management.Setup
{
    public partial class MasterSession : libForm
    {
        private LibManagementEntities context;
        private UpdateType eUpdateType;
        private Session OSession;
        private enum UpdateType { New = 0, Edit = 1 }
        public MasterSession()
        {
            InitializeComponent();
            context = new LibManagementEntities();
        }

        private void MasterSession_Load(object sender, EventArgs e)
        {
            txtSessionName.Enabled = false;
            dtpStartDate.Enabled = false;
            dtpEndDate.Enabled = false;
            btnSave.Enabled = false;

            lsbSession.SetDataSource(context.Sessions.ToList(), "SessionName", "SessionId");
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            btnEdit.Enabled = false;
            dtpStartDate.Enabled = true;
            dtpEndDate.Enabled = true;
            lsbSession.Enabled = false;
            btnSave.Enabled = true;
            eUpdateType = UpdateType.New;
            txtSessionName.Clear();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnNew.Enabled = false;
            lsbSession.Enabled = false;
            dtpStartDate.Enabled = true;
            dtpEndDate.Enabled = true;
            btnSave.Enabled = true;
            eUpdateType = UpdateType.Edit;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtSessionName.Clear();
            txtSessionName.Enabled = false;
            dtpStartDate.Enabled = false;
            dtpEndDate.Enabled = false;
            btnEdit.Enabled = true;
            btnSave.Enabled = true;
            btnNew.Enabled = true;
            lsbSession.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                context = new LibManagementEntities();
                btnNew.Enabled = true;
                btnEdit.Enabled = true;
                lsbSession.Enabled = true;
                if (eUpdateType == UpdateType.New)
                {
                    OSession = new Session();
                    OSession.SessionName = txtSessionName.Text;
                    OSession.SessionStartDate = dtpStartDate.Value.Date;
                    OSession.SessionEndDate = dtpEndDate.Value.Date;
                    OSession.ActiveSession = false;
                    context.Sessions.Add(OSession);
                }
                else if (eUpdateType == UpdateType.Edit)
                {
                    OSession.SessionName = txtSessionName.Text;
                    OSession.SessionStartDate = dtpStartDate.Value.Date;
                    OSession.SessionEndDate = dtpEndDate.Value.Date;
                }
                context.SaveChanges();
                btnClear_Click(sender, e);
                MasterSession_Load(sender, e);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            txtSessionName.Text = dtpStartDate.Value.Date.ToString("MMM", CultureInfo.InvariantCulture) + "-" + dtpStartDate.Value.Year + " To " + dtpEndDate.Value.Date.ToString("MMM", CultureInfo.InvariantCulture) + "-" + dtpEndDate.Value.Year;
        }

        private void dtpEndDate_ValueChanged(object sender, EventArgs e)
        {
            txtSessionName.Text = dtpStartDate.Value.Date.ToString("MMM", CultureInfo.InvariantCulture) + "-" + dtpStartDate.Value.Year + " To " + dtpEndDate.Value.Date.ToString("MMM", CultureInfo.InvariantCulture) + "-" + dtpEndDate.Value.Year;
        }

        private void lsbSession_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsbSession.SelectedIndex != -1)
            {
                try
                {
                    OSession = new Session();
                    OSession = context.Sessions.Single(x => x.SessionId == (decimal)lsbSession.SelectedValue);
                    dtpStartDate.Value = OSession.SessionStartDate;
                    dtpEndDate.Value = OSession.SessionEndDate;
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
